<template>
  <div>
    <div style="margin-bottom: 5%;"></div>
    <el-card style="margin:0 auto;width: 60%;">
      <div style="margin-bottom: 2%;"></div>
      <div style="font-weight: initial ;font-size: 35px;text-align:center;">新 增 学 院</div>
      <div style="margin-bottom: 5%;"></div>

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="30%">
        <el-form-item  label="学院编号">
          <el-input-number v-model="ruleForm.academyId" :min="1" :max="9999"></el-input-number>
        </el-form-item>
        <div style="margin-bottom: 4%;"></div>
        <el-form-item label="学院名称" prop="academyName">
          <el-input v-model="ruleForm.academyName" placeholder="例如:XXX学院" style="width: 60%"></el-input>
        </el-form-item>
        <div style="margin-bottom: 4%;"></div>
        <el-form-item label="学院地址" prop="academyAddress">
          <el-input type="textarea" :autosize="{ minRows: 3}" style="width:60%" v-model="ruleForm.academyAddress"></el-input>
        </el-form-item>
        <div style="margin-bottom: 4%;"></div>
        <el-form-item label="学院简介" prop="academySummary">
          <el-input type="textarea" :autosize="{ minRows: 6}" style="width:60%" v-model="ruleForm.academySummary"></el-input>
        </el-form-item>
        <el-form-item>
          <span>暂不启用 </span>
          <el-switch
              v-model="ruleForm.academyFlag"
              active-color="#13ce66"
              inactive-color="#999999"
              active-value="0"
              inactive-value="1">
          </el-switch>
          <span> 立即启用</span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即新增</el-button>
          <el-button style="margin-left:60px" @click="returnPage()">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm:{
        academyId:'',
        academyName:'',
        academyAddress:'',
        academySummary:'',
        academyFlag:''
      },
      rules: {
        academyName: [
          { required: true, message: '请输入学院名称', trigger: 'blur' },
          { min: 2, max: 40, message: '请输入 2 到 40 个字符!', trigger: 'blur' }
        ],
        academyAddress: [
          { min: 0, max: 120, message: '您输入的学院地址太长了!', trigger: 'blur' }
        ],
        academySummary: [
          { min: 0, max: 400, message: '您输入的学院简介太长了!', trigger: 'blur' }
        ]
      }
    }
  },
  created(){

  },
  methods:{
    submitForm(formName){
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          console.log(this.ruleForm);
          let path = 'http://localhost:8081/academy/insertOneAcademy';
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message({
                showClose: true,
                message: res.data.message,
                type: 'success'
              });
            }
            else{
              this.$message({
                showClose: true,
                message: res.data.message,
                type: 'error'
              });
            }
          })
        }
      });
    },
    returnPage(){
      this.$router.push('/admin/academyMGMT');
    }
  }
}
</script>

<style scoped>

</style>